//
//  SettingVC.m
//  DriverClient
//
//  Created by 吕晶 on 2020/8/3.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "SettingVC.h"
#import "SettingCell.h"
#import "SettingLogOutCell.h"
#import "PersonInfoVC.h"
#import "VerifyPwdAlertView.h"
#import "SetNewPwdVC.h"
#import "ModifyPhoneVC.h"
#import "ModifyEmailVC.h"

@interface SettingVC ()<UITableViewDelegate,UITableViewDataSource> {
    BOOL isSetEmail;
    NSString *phone;
    NSString *email;
}

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation SettingVC

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - BottomHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    isSetEmail = NO;
    phone = @"";
    email = @"";
    [self.view addSubview:self.tableView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestForInfo];
}

- (void)requestForInfo {
    [HttpRequest PostWithApi:ReceiptDeliveryPersonInfo params:@{} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self->isSetEmail = [[responseObject objectForKey:@"isemail"] intValue] == 1 ? YES : NO;
            self->phone = [responseObject objectForKey:@"phone"];
            self->email = [responseObject objectForKey:@"email"];
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SettingCell *cell = [SettingCell SettingCellWithTableView:tableView];
        if (indexPath.row == 0) {
            cell.titleText = @"个人资料";
            cell.descText = @"";
            cell.showLine = YES;
        }else if (indexPath.row == 1) {
            cell.titleText = @"密码修改";
            cell.descText = @"";
            cell.showLine = YES;
        }else if (indexPath.row == 2) {
            cell.titleText = @"邮箱绑定";
            cell.descText = isSetEmail ? email : @"未设置";
            cell.showLine = YES;
        }else {
            cell.titleText = @"手机绑定";
            cell.descText = phone;
            cell.showLine = NO;
        }
        return cell;
    }else {
        SettingLogOutCell *cell = [SettingLogOutCell SettingLogOutCellWithTableView:tableView];
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SettingCell *cell = [SettingCell SettingCellWithTableView:tableView];
        return cell.height;
    }else {
        SettingLogOutCell *cell = [SettingLogOutCell SettingLogOutCellWithTableView:tableView];
        return cell.height;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            PersonInfoVC *vc = [PersonInfoVC new];
            [self.rt_navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 1) {
            VerifyPwdAlertView *alert = [VerifyPwdAlertView new];
            __weak_self;
            alert.sureBlock = ^{
                SetNewPwdVC *vc = [SetNewPwdVC new];
                [weakself.rt_navigationController pushViewController:vc animated:YES];
            };
            [alert showInView:MAIN_WINDOW];
        }else if (indexPath.row == 2) {
//            if (isSetEmail) {
                VerifyPwdAlertView *alert = [VerifyPwdAlertView new];
                __weak_self;
                alert.sureBlock = ^{
                    __strong_self;
                    ModifyEmailVC *vc = [ModifyEmailVC new];
                    vc.email = strongSelf->email;
                    [strongSelf.rt_navigationController pushViewController:vc animated:YES];
                };
                [alert showInView:MAIN_WINDOW];
//            }else {
//                [MBProgressHUD leShowFailedHUD:self.view message:@"请先设置密码"];
//            }
        }else if (indexPath.row == 3) {
//            if (isSetPwd) {
                VerifyPwdAlertView *alert = [VerifyPwdAlertView new];
                __weak_self;
                alert.sureBlock = ^{
                    __strong_self;
                    ModifyPhoneVC *vc = [ModifyPhoneVC new];
                    vc.phone = strongSelf->phone;
                    [strongSelf.rt_navigationController pushViewController:vc animated:YES];
                };
                [alert showInView:MAIN_WINDOW];
//            }else {
//                [MBProgressHUD leShowFailedHUD:self.view message:@"请先设置密码"];
//            }
        }
//        if (indexPath.row == 0) {
//            PersonInfoVC *vc = [PersonInfoVC new];
//            [self.rt_navigationController pushViewController:vc animated:YES];
//        }else if (indexPath.row == 1) {
//            if (isSetPwd) {
//                VerifyPwdAlertView *alert = [VerifyPwdAlertView new];
//                __weak_self;
//                alert.sureBlock = ^{
//                    SetNewPwdVC *vc = [SetNewPwdVC new];
//                    [weakself.rt_navigationController pushViewController:vc animated:YES];
//                };
//                [alert showInView:MAIN_WINDOW];
//            }else {
//                SetNewPwdVC *vc = [SetNewPwdVC new];
//                [self.rt_navigationController pushViewController:vc animated:YES];
//            }
//        }else if (indexPath.row == 3) {
//            if (isSetPwd) {
//                VerifyPwdAlertView *alert = [VerifyPwdAlertView new];
//                __weak_self;
//                alert.sureBlock = ^{
//                    __strong_self;
//                    ModifyPhoneVC *vc = [ModifyPhoneVC new];
//                    vc.phone = strongSelf->phone;
//                    [strongSelf.rt_navigationController pushViewController:vc animated:YES];
//                };
//                [alert showInView:MAIN_WINDOW];
//            }else {
//                [MBProgressHUD leShowFailedHUD:self.view message:@"请先设置密码"];
//            }
//        }
    }else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定退出登录？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [User logoutSuccess];
        }];
        UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:sureAction];
        [alert addAction:cancleAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

@end
